<?php
/**
 * common repository file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/12
 * Time: 10:42
 */

namespace HiCommon\Repository;

use DB;
use HiCommon\Model\OrderMall;

class OrderMallRepository extends BaseRepository
{
    public function base_model()
    {
        return new \HiCommon\Model\OrderMallRepository();
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        $model = new OrderMall();
        if (!empty($arr_option['where']['range'])) {
            $range = explode(' - ', $arr_option['where']['range']);
            $model = $model->whereBetween('created_at', [$range[0], $range[1] . ' 23:59:59']);
            unset($arr_option['where']['range']);
        }
        return $model->commonget($arr_option);
    }

    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list($arr_option = [], array $arr_search = [])
    {
        return OrderMall::where(
            function ($query)
            use ($arr_search) {
                if (!empty($arr_search['order_no'])) {
                    $query->where('order_no', 'like', '%' . $arr_search['order_no'] . '%');
                }

                if ($arr_search['pay_status'] != '') {
                    $query->where('pay_status', $arr_search['pay_status']);
                }
                if ($arr_search['status'] != '') {
                    $query->where('status', $arr_search['status']);
                }

                if (!empty($arr_search['date_time'])) {
                    $created_at = DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d')");
                    $arr_select = ['*', "DATE_FORMAT(created_at,'%Y-%m-%d')"];

                    $query->select($arr_select)->where($created_at, $arr_search['date_time']);
                }

            })->commonpage($arr_option);
    }

    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where, $arr_option = [])
    {
        $model = new OrderMall();
        if (!empty($arr_where['created_at'])) {
            $model = $model->where('created_at', '>=', $arr_where['created_at']);
            $model = $model->where('created_at', '<=', $arr_where['created_at'] . ' 23:59:59');
            unset($arr_where['created_at']);
        }
        if (!empty($arr_where['range'])) {
            $range = explode(' - ', $arr_where['range']);
            $model = $model->whereBetween('created_at', [$range[0], $range[1] . ' 23:59:59']);
            unset($arr_where['range']);
        }

        return $model->where($arr_where)->commonpage($arr_option);
    }


    /**
     * 个人中心统计积分订单的数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_whereIn( $arr_where )
    {
        return OrderMall::where( $arr_where )->whereIn('status' , [ 1 , 2 ]  )->count();
    }


    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return OrderMall::where($arr_where)->count();
    }

    //获取正在进行中的订单
    public function count_order($arr_where)
    {
        return OrderMall::where($arr_where)->where('status', '<', 4)->count();
    }

    /**
     * 根据条件统计总和
     *
     * @param array $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [], $s_field = 'mark')
    {
        return OrderMall::where($arr_where)->sum($s_field);
    }

    /**
     * 根据条件批量删除信息
     *
     * @param array $arr_where
     * @param bool $method
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_by_where($arr_where = [], $method = false)
    {
        return OrderMall::where($arr_where)->commondelete($method);
    }

    /**
     * 通过where获取一条数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_by_where($arr_where, $arr_option = [])
    {
        return OrderMall::where($arr_where)->commonfirst($arr_option);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return OrderMall::create($arr_input);
    }

    /**
     * 添加多条数据
     *
     * @param array $arr_data
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_many($arr_data)
    {
        return OrderMall::insert($arr_data);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return OrderMall::commonfind($n_id, $arr_option);
    }

    /**
     * 更新或新建一条数据
     *
     * @param $arr_where
     * @param $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_or_create($arr_where, $arr_input)
    {
        $res = OrderMall::where($arr_where)->first();
        if (is_null($res)) {
            OrderMall::create(array_merge($arr_where, $arr_input));
        } else {
            OrderMall::where($arr_where)->update($arr_input);
        }
        return OrderMall::where($arr_where)->first();
    }

    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $map, array $arr_input)
    {
        return OrderMall::where($map)->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return OrderMall::where('id', $n_id)->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id, array $up_data)
    {
        return OrderMall::where('id', $n_id)->update($up_data);

    }

    /**
     * 字段自增
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return OrderMall::where('id', $n_id)->increment($s_field, $n_step);
    }

    /**
     * 字段自减
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return OrderMall::where('id', $n_id)->decrement($s_field, $n_step);
    }

    /**
     *  订单状态判断
     *  yang 2019 11 04
     **/
    public function order_status($status, $pay_status)
    {
        $state = '';
        if ($status == 1 && $pay_status == 1) {
            $state = '待支付';
        } elseif ($status == 1 && $pay_status == 2) {
            $state = '待发货';
        } elseif ($status == 2) {
            $state = '待收货';
        } elseif ($status == 3) {
            $state = '兑换成功';
        } elseif ($status == 4) {
            $state = '已评价';
        } elseif ($status == 5) {
            $state = '已取消';
        }

        return $state;
    }


    /**
     * 根据条件查询订单
     */
    public function get_one_where(array $map, $option = [])
    {
        return OrderMall::where($map)->with(['getOrderMallDetail'])->first($option);
    }


    /**
     * 通过条件查询所有可自动取消的订单(定时任务)
     */
    public function all_auto_cancel_order($arr_select = ['*'], $n_minutes = 30)
    {
        $arr_where = ['status' => 1, 'pay_status' => 1];
        $s_time = time_to_ymdhis(time() - 60 * $n_minutes);

        return OrderMall::where($arr_where)->where('created_at', '<', $s_time)->select($arr_select)->get();
    }


    /**
     * 根据条件统计订单数量(公众号)
     */
    public function count_by_where(int $shop_id, int $time_cate, array $dayArray)
    {
        if ($time_cate == 2) {
            $raw = DB::raw("count(1) as total, DATE_FORMAT(created_at,'%Y-%m') as date ,sum(order_integral) as integral");
            $between = [reset($dayArray), date('Y-m-d H:i:s', time())];
        } else {
            $raw = DB::raw("count(1) as total, DATE_FORMAT(created_at,'%Y-%m-%d') as date,sum(order_integral) as integral");
            $between = [reset($dayArray), end($dayArray) . ' 23:59:59'];
        }

        //订单统计
        $days[1] = OrderMall::select($raw)
            ->whereBetween('created_at', $between)
            ->where(['shop_id' => $shop_id])
            ->groupBy('date')
            ->get()->toArray();

        //总共的订单数
        $days[3] = OrderMall::whereBetween('created_at', $between)
            ->where(['shop_id' => $shop_id])
            ->count();

        return $days;
    }
}